import asyncio
import websockets
import json

def push_to_frontend(message, ws_url="ws://localhost:8000/ws"):
    async def _push():
        try:
            async with websockets.connect(ws_url) as websocket:
                await websocket.send(json.dumps({"text": message}))
                print("已推送到前端。")
        except Exception as e:
            print("推送前端失败：", e)
    asyncio.run(_push()) 